package com.by.gateway.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@ConfigurationProperties(prefix = "gateway")
public class GatewayConfig {
    /**
     * 路由配置，key为路由前缀，value为目标服务地址
     * 例如: /user -> user-service:8081
     */
    private Map<String, String> routes = new HashMap<>();

    public Map<String, String> getRoutes() {
        return routes;
    }

    public void setRoutes(Map<String, String> routes) {
        this.routes = routes.entrySet().stream()
                .collect(Collectors.toMap(
                        entry -> entry.getKey().startsWith("/") ? entry.getKey() : "/" + entry.getKey(),
                        Map.Entry::getValue,
                        (existing, replacement) -> existing, // 处理键冲突的合并策略
                        HashMap::new // 保持插入顺序
                ));
    }
}